package cn.com.capinfo.ea.example.demo.controller



import cn.com.capinfo.ea.example.demo.domain.Author
import cn.com.capinfo.ea.example.demo.service.AuthorService
import cn.com.capinfo.ea.example.demo.service.core.UtilService
import cn.com.capinfo.ea.core.CapinfoSpringUtils
import cn.com.capinfo.ea.core.PageParams
import cn.com.capinfo.ea.plugin.poi.ExcelWriteBuilder
import cn.com.capinfo.ea.core.annotation.CapinfoController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.ResponseBody
import groovy.util.logging.Slf4j
import org.springframework.transaction.annotation.Transactional
/**
 * Generate by CapinfoTools EA-3.7.0 on 2020-07-27.
 */
@Slf4j
@Transactional
@CapinfoController
class AuthorController {
    @Autowired private AuthorService authorService
    @Autowired private UtilService utilService
    public void index(){

    }

    @ResponseBody
    public Map json(PageParams pageParams,String search){
        Map map = new HashMap();
        Closure searchLogic={
            if(search){
                //like('name',"%${search}%")
            }
        }
        long count = authorService.count(searchLogic)
        List list = authorService.list(pageParams,searchLogic)
        map.total = count;
        map.rows = list;
        return map;
    }

    public void show(long id,Model model){
        Author authorInstance = authorService.read(id)
        model.addAttribute("authorInstance",authorInstance);
    }

    public void create(Model model){
        Author authorInstance =authorService.create()
        model.addAttribute("authorInstance",authorInstance);
        
    }

    @ResponseBody
    public Map save(Author authorInstance){
        Map map=new HashMap();
        if (authorInstance == null) {
            notFound(map,"");
            return map;
        }

        if (authorInstance.hasErrors()) {
            foundErrors(authorInstance,map);
            return map;
        }

        if (!authorService.save(authorInstance)) {
            foundErrors(authorInstance,map);
        }else{
            map.result = true;
            map.id=authorInstance.id;
            String domainName=CapinfoSpringUtils.getI18nMessage("author.label");
            map.message = CapinfoSpringUtils.getI18nMessage("default.created.message",[domainName,authorInstance.id]);
        }
        return map;
    }

    public void edit(long id,Model model){
        Author authorInstance = authorService.read(id)
        model.addAttribute("authorInstance",authorInstance);
        
    }

    @ResponseBody
    public Map update(Author authorInstance,Long version){
        Map map=new HashMap();
        if (authorInstance== null) {
            notFound(map,"${authorInstance.id}")
            return map
        }
        if (version != null) {
            if (authorInstance.version>version) {
                map.result=false
                map.message = CapinfoSpringUtils.getI18nMessage("default.optimistic.locking.failure",[authorInstance.id]);
                return map;
            }
        }
        if (authorInstance.hasErrors()) {
            foundErrors(authorInstance,map);
            return map
        }
        boolean result=authorService.save(authorInstance)
        if (!result) {
            foundErrors(authorInstance,map);
        }else{
            map.result = true;
            String domainName=CapinfoSpringUtils.getI18nMessage("author.label");
            map.message = CapinfoSpringUtils.getI18nMessage("default.updated.message",[domainName,authorInstance.id]);
        }
        return map;
    }

    @ResponseBody
    public Map delete(long id){
        Map map=new HashMap();
        Author authorInstance=Author.get(id);
        if (authorInstance == null) {
            notFound(map,"${id}");
            return map;
        }
        String domainId = authorInstance.id.toString()
        boolean result=authorService.delete(authorInstance)
        map.result = result
        String domainName = CapinfoSpringUtils.getI18nMessage("author.label");
        if(result){
            map.message = CapinfoSpringUtils.getI18nMessage("default.deleted.message", [domainName, domainId]);
        }else{
            map.message = CapinfoSpringUtils.getI18nMessage("default.not.deleted.message", [domainName, domainId]);
        }
        return map;
    }

    @ResponseBody
    public Map deletes(String ids,Model model){
        Map map=new HashMap();
        String domainName = CapinfoSpringUtils.getI18nMessage("author.label");
        boolean result=authorService.deletes(ids)
        map.result = result;
        if(result){
            map.message = CapinfoSpringUtils.getI18nMessage("default.deleted.message", [domainName, ids]);
        }else{
            map.message = CapinfoSpringUtils.getI18nMessage("default.not.deleted.message", [domainName, ids]);
        }
        return map;
    }


    public void download(javax.servlet.http.HttpServletResponse response){
        File tempFile=File.createTempFile("_tmp",".xls")
        //开发模式获取模板文件
        URL templateFileResource=ClassLoader.getResource("/templates/tools/empty.xls")
        if(templateFileResource){
            tempFile.bytes=new File(templateFileResource.toURI()).bytes
        }else{
            //jar包模式下获取模板文件
            tempFile.bytes=this.class.classLoader.getResourceAsStream("/templates/tools/empty.xls")?.bytes
        }
        ExcelWriteBuilder excelWriteBuilder = new ExcelWriteBuilder(tempFile);
        excelWriteBuilder.workbook {
            sheet("sheet1") {
                row(["id","version", "info"]);
                authorService.list(new PageParams(max:100),{}).each{Author authorInstance->
                    row([authorInstance.id,authorInstance.version,authorInstance.toString()])
                }
            }
        }
        byte[] excelBytes=excelWriteBuilder.download();
        try{
            response.setContentType("application/octet-stream;charset=GBK")
            response.addHeader("Content-Disposition", 'attachment; filename="' + new String("文件导出Excel.xls".getBytes("GBK"),"ISO8859-1")+'"');
            OutputStream out = response.outputStream;
            out.write(excelBytes);
            out.close();
        }catch(e){
            log.error(e.message);
            response.writer.print(e.message);
        }
    }




    protected Map notFound(Map map,String id) {
        String domainName=CapinfoSpringUtils.getI18nMessage("author.label");
        map.result=false;
        map.message = CapinfoSpringUtils.getI18nMessage("default.not.found.message",[domainName,id]);
        return map;
    }
    protected Map foundErrors(Author authorInstance,Map map){
        map.result = false;
        map.errors = utilService.collectionErrorMap(Author,authorInstance);
        return map;
    }
}

